package com.shiro.receiver.framework.configuration;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.shiro.receiver.framework.filter.WebFilter;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Configuration
@RequiredArgsConstructor
public class FilterConfiguration {

    private final List<WebFilter> webFilterList;

    @Bean
    public List<FilterRegistrationBean<WebFilter>> filterRegistrationBean() {
        List<FilterRegistrationBean<WebFilter>> resultList = new ArrayList<>();
        if (CollUtil.isEmpty(webFilterList)) {
            return resultList;
        }
        for (WebFilter webFilter : webFilterList) {
            FilterRegistrationBean<WebFilter> registrationBean = new FilterRegistrationBean<>();
            registrationBean.setFilter(webFilter);
            registrationBean.setUrlPatterns(webFilter.getPathPatterns());
            registrationBean.setOrder(webFilter.getOrder());
            resultList.add(registrationBean);
            log.info("[过滤器注册成功:{}] [优先级:{}] [匹配路径:{}]", webFilter.getName(), webFilter.getOrder(), JSONObject.toJSONString(webFilter.getPathPatterns()));
        }
        return resultList;
    }

}
